// JavaScript Document
//分页每页大小
var pageRows = 10;

/**
 * @return 项目的根
 */
function getRootPath() {
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPath + projectName);
};

//页头下拉菜单
$(function () {
	$(".dropdown-switch").hover(function () {
		$(this).find(".menu-down").show();
	}, function () {
		$(this).find(".menu-down").hide();
	});
   //消息
    getmescount();
})

//返回顶部
jQuery(document).ready(function ($) {
	if ($("meta[name=toTop]").attr("content") == "true") {
		$("<div class='upTop-text'></div>").appendTo('body');
		$("#upTop").css({
			width: '45px',
			height: '45px',
			bottom: '15px',
			right: '15px',
			position: 'fixed',
			cursor: 'pointer',
			zIndex: '999999',
		});
		if ($(this).scrollTop() == 0) {
			$("#upTop").hide();
		}
		$(window).scroll(function (event) {
			/* Act on the event */
			if ($(this).scrollTop() == 0) {
				$("#upTop").hide();
			}
			if ($(this).scrollTop() != 0) {
				$("#upTop").show();
			}
		});
		$("#upTop").click(function (event) {
			/* Act on the event */
			$("html,body").animate({
					scrollTop: "0px"
				},
				666
			)
		});
	}
});


//类别导航点击切换
$(function () {
	var $div_li = $(".main-nav li");
	$div_li.click(function () {
		$(this).addClass("active") 
			.siblings().removeClass("active");
	})
})


//商品列表筛选条件-排序条件
$(function () {
	$(".sort-item").click(function () {
		$(this).addClass("sort-active").siblings().removeClass("sort-active");
		var p1 = $("#pai1").children("img")
		var hP = $("#hole-price").children("img")
		var pP = $("#pre-price").children("img")
		var aL = $("#area").children("img")
		var patt2 = new RegExp("up")
		if (patt2.test(p1.attr("src")) == true) {
			p1.attr("src", "images/sort-up-defult.png")
		} else {
			p1.attr("src", "images/sort-down-defult.png")
		} 

		if (patt2.test(hP.attr("src")) == true) {
			hP.attr("src", "images/sort-up-defult.png")
		} else {
			hP.attr("src", "images/sort-down-defult.png")
		} 
		if (patt2.test(pP.attr("src")) == true) {
			pP.attr("src", "images/sort-up-defult.png")
		} else {
			pP.attr("src", "images/sort-down-defult.png")
		} 

		if (patt2.test(aL.attr("src")) == true) {
			aL.attr("src", "images/sort-up-defult.png")
		} else {
			aL.attr("src", "images/sort-down-defult.png")
		} 

	});

	var p1 = $("#pai1").children("img")
	var hP = $("#hole-price").children("img")
	var pP = $("#pre-price").children("img")
	var aL = $("#area").children("img")

	$("#pai1").click(function () {
		var patt1 = new RegExp("up")
		if (patt1.test(p1.attr("src")) == true) {
			p1.attr("src", "images/sort-up-defult.png")
		} else {
			p1.attr("src", "images/sort-down-defult.png")
		} 

		if (patt1.test(aL.attr("src")) == true) {
			aL.attr("src", "images/sort-up-defult.png")
		} else {
			aL.attr("src", "images/sort-down-defult.png")
		} 

		if (p1.attr("src") == "images/sort-up-defult.png") {
			p1.attr("src", "images/sort-down-active.png");
		} else if (p1.attr("src") == "images/sort-down-active.png") {
			p1.attr("src", "images/sort-up-active.png");
		} else if (p1.attr("src") == "images/sort-up-active.png") {
			p1.attr("src", "images/sort-down-active.png");
		} else {
			p1.attr("src", "images/sort-up-active.png")
		}

	});

	$("#hole-price").click(function () {
		var patt1 = new RegExp("up")
		if (patt1.test(pP.attr("src")) == true) {
			pP.attr("src", "images/sort-up-defult.png")
		} else {
			pP.attr("src", "images/sort-down-defult.png")
		} 

		if (patt1.test(aL.attr("src")) == true) {
			aL.attr("src", "images/sort-up-defult.png")
		} else {
			aL.attr("src", "images/sort-down-defult.png")
		} 

		if (hP.attr("src") == "images/sort-up-defult.png") {
			hP.attr("src", "images/sort-down-active.png");
		} else if (hP.attr("src") == "images/sort-down-active.png") {
			hP.attr("src", "images/sort-up-active.png");
		} else if (hP.attr("src") == "images/sort-up-active.png") {
			hP.attr("src", "images/sort-down-active.png");
		} else {
			hP.attr("src", "images/sort-up-active.png")
		}

	});

	$("#pre-price").click(function () {
		var patt1 = new RegExp("up")
		if (patt1.test(hP.attr("src")) == true) {
			hP.attr("src", "images/sort-up-defult.png")
		} else {
			hP.attr("src", "images/sort-down-defult.png")
		} 

		if (patt1.test(aL.attr("src")) == true) {
			aL.attr("src", "images/sort-up-defult.png")
		} else {
			aL.attr("src", "images/sort-down-defult.png")
		} 

		if (pP.attr("src") == "images/sort-down-defult.png") {
			pP.attr("src", "images/sort-up-active.png");
		} else if (pP.attr("src") == "images/sort-up-active.png") {
			pP.attr("src", "images/sort-down-active.png");
		} else if (pP.attr("src") == "images/sort-down-active.png") {
			pP.attr("src", "images/sort-up-active.png");
		} else {
			pP.attr("src", "images/sort-down-active.png")
		}
	});

	$("#area").click(function () {
		var patt1 = new RegExp("up")
		if (patt1.test(hP.attr("src")) == true) {
			hP.attr("src", "images/sort-up-defult.png")
		} else {
			hP.attr("src", "images/sort-down-defult.png")
		} 

		if (patt1.test(pP.attr("src")) == true) {
			pP.attr("src", "images/sort-up-defult.png")
		} else {
			pP.attr("src", "images/sort-down-defult.png")
		} 

		if (aL.attr("src") == "images/sort-up-defult.png") {
			aL.attr("src", "images/sort-down-active.png");
		} else if (aL.attr("src") == "images/sort-down-active.png") {
			aL.attr("src", "./images/sort-up-active.png");
		} else if (aL.attr("src") == "images/sort-up-active.png") {
			aL.attr("src", "images/sort-down-active.png");
		} else {
			aL.attr("src", "images/sort-up-active.png")
		}
	});
});
Date.prototype.format = function(format){
	var o = {
		"M+" : this.getMonth()+1, //month
		"d+" : this.getDate(), //day
		"h+" : this.getHours(), //hour
		"m+" : this.getMinutes(), //minute
		"s+" : this.getSeconds(), //second
		"q+" : Math.floor((this.getMonth()+3)/3), //quarter
		"S" : this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
		}
	}
	return format;
}

function alert(message){
    $('#commontAlter #content').html(message)
    $('#commontAlter').modal('show');
    $('#commontAlter').on('hide.bs.modal', function () {
    })
}

function getmescount(){
    $.ajax({
        type: "get",
        url: "/custom/message/getusermessage",
//                cache:false,
//                async:false,
//             dataType: "json",
        success: function(data){
            $("#usermessagecount").html(data);
        },
        failure:function (result) {
            console.log("消息总数获取失败");
        }
    });
}
window.setInterval("getmescount()",10000);

function getQueryString(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)","i");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null){
        var sname = r[2];
        if(sname!=null){
            var sname_ = decodeURIComponent(sname);
            return sname_;
        }else{
            return null;
		}
	}else{
        return null;
	}
}
